package com.jerry.proxy;

import java.lang.reflect.Method;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

public class CglibProxy implements MethodInterceptor {
	
	private Object target; // 目标对象
	
	public CglibProxy(Object target){
		this.target = target;
	}
	
	
	public Object getInstance(){// 给目标对象 创建一个代理对象
		Enhancer hander = new Enhancer();// 1工具类
		
		hander.setSuperclass(target.getClass());// 2设置父类
		
		hander.setCallback(this); // 设置回调函数
		
		return hander.create();
	}
	

	public Object intercept(Object obj, Method method, Object[] objs,
			MethodProxy arg3) throws Throwable {
		System.out.println("1.打开冰箱门");
		
		Object result = method.invoke(target, objs);
		
		System.out.println("2:关闭冰箱大门");
		return result;
	}

}
